package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.controller.dto.RoleDto;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 角色控制器
 * @Author makun
 * @Date 2023/9/27 10:36
 * @Version 1.0
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 获取所有的角色
     * @param queryString
     * @return
     */
    @GetMapping("/all")
    public Result getAll(String queryString) {
        return Result.buildSuccess(roleService.getAll(queryString));
    }

    /**
     * 编辑一个角色
     * @param roleDto
     * @return
     */
    @PutMapping
    public Result edit(@RequestBody RoleDto roleDto) {
        return roleService.editOne(roleDto) ? Result.buildSuccess() : Result.buildFail();
    }

    /**
     * 获取角色下所有的菜单ID
     * @param id
     * @return
     */
    @GetMapping({"/menus","/getMenuIdsByRoleId.do"})
    public Result getMenuIdsByRoleId(Integer id) {
        return Result.buildSuccess(roleService.getMenuIdsByRoleId(id));
    }

    /**
     * 添加角色
     * @param roleDto
     * @return
     */
    @PostMapping
    public Result saveOne(@RequestBody RoleDto roleDto) {
        return roleService.saveOne(roleDto) ? Result.buildSuccess() : Result.buildFail();
    }

    /**
     * 分页查询角色列表
     * @param queryPageBean
     * @return
     */
    @GetMapping
    public Result getRoles(QueryPageBean queryPageBean) {
        Page<Role> rolePage = roleService.findPage(queryPageBean);
        return Result.buildSuccess(rolePage);
    }
}
